const { Controller } = require('egg');
const validate = require('../rule/rule')

class HomeController extends Controller {

  /**
   * 
   * @api { GET } /user/permission/list 获取权限列表
   * @apiName list
   * @apiGroup permission
   * @apiVersion 1.0.0
   * 
   * @apiRequest json
   * @apiQuery { String } name 权限名称
   * @apiQuery { String } key 权限关键key
   * @apiQuery { String } url 地址
   * @apiQuery { String } order 排序字段
   * @apiQuery { String } method 排序方式
   * @apiQuery { Number } pageIndex 页码
   * @apiQuery { Number } pageSize 页面容量
   * 
   * @apiHeader { String } Authorization 注册信息
   */
  async list(req, res) {
    const ctx = this.ctx;
    try {
      const { query } = req
      const { name, key, url, order, method, pageIndex, pageSize } = validate.permissionListValidate(query);
      const data = ctx.service.permission.list(name, key, url, order, method, pageIndex, pageSize)
      ctx.status = 200;
      ctx.sendSuccess({ data });
      return;
    } catch (error) {
      ctx.status = 500;
      ctx.sendFailed({ msg: error.message });
      return;
    }
  }

  /**
   * 
   * @api { POST } /user/permission/insert 新增权限
   * @apiName insert
   * @apiGroup permission
   * @apiVersion 1.0.0
   * 
   * @apiRequest json
   * @apiParam { String } name 名称
   * @apiParam { String } key 关键key
   * @apiParam { String } url 地址
   * @apiParam { Number } type 类型 1.后端 2.前端
   * @apiParam { String } parent_id 父id
   * 
   * 
   */
  async insert(req, res) {
    const ctx = this.ctx;
    try {
      const { body } = req.request
      const { name, key, url, type, parent_id } = validate.permissionInsertValidate(body);
      const data = ctx.service.permission.insert(name, key, url, type, parent_id)
      ctx.status = 200;
      ctx.sendSuccess({ data });
      return;
    } catch (error) {
      ctx.status = 500;
      ctx.sendFailed({ msg: error.message });
      return;
    }
  }

  /**
   * 
   * @api { POST } /user/permission/update 更新权限
   * @apiName update
   * @apiGroup permission
   * @apiVersion 1.0.0
   * 
   * @apiRequest json
   * @apiParam { String } name 名称
   * @apiParam { String } key 关键key
   * @apiParam { String } url 地址
   * @apiParam { String } uuid uuid
   * 
   * 
   */
  async update(req, res) {
    const ctx = this.ctx;
    try {
      const { body } = req.request
      const { name, key, url, uuid } = validate.permissionUpdateValidate(body);
      const data = ctx.service.permission.update(name, key, url, uuid)
      ctx.status = 200;
      ctx.sendSuccess({ data });
      return;
    } catch (error) {
      ctx.status = 500;
      ctx.sendFailed({ msg: error.message });
      return;
    }
  }

  /**
   * 
   * @api { POST } /user/permission/delete 删除权限
   * @apiName delete
   * @apiGroup permission
   * @apiVersion 1.0.0
   * 
   * @apiRequest json
   * @apiParam { Object } ids id
   * 
   * @apiHeader { String } Authorization 注册信息
   * 
   */
  async delete(req, res){
    const ctx = this.ctx;
    try {
      const { body } = req.request
      const { ids } = validate.permissionDeleteValidate(body);
      const data = ctx.service.permission.delete(ids)
      ctx.status = 200;
      ctx.sendSuccess({ data });
      return;
    } catch (error) {
      ctx.status = 500;
      ctx.sendFailed({ msg: error.message });
      return;
    }
  }
}

module.exports = HomeController;
